<?php
//------------------------------------------------------------ Contraintes d'accès de la page

	// on vérifie que cette page est appelée à partir de l'index et que l'utilisateur a le droit de consultation
	if(isset($_SESSION) && ($GLOBALS['USER']['level'] >= 2 || $GLOBALS['USER']['admin'])){
	
		// l'identifiant de la donnée à supprimer est indiqué par un entier
		if(isset($_GET['id']) && numericInt($_GET['id'])){
		
			$nb_visites = valueExists((int)$_GET['id'], "CODEVISITEDANSNATURE", "visite_dans_nature", $connexion);
			
			if($nb_visites == 1){ // la visite à afficher existe et son identifiant est unique
	
?>
<?php
/********************************* Règles :

	- id doit désigner une seule visite
	- La visite doit exister dans la BDD

*****************************************/

	// récupération des informations de la visite
	$query_recuperation_visite = "SELECT v.DATEVISITE AS date, v.RAPPORT AS rapport, v.DESCRIPTION AS description, v.COMMENTAIRE AS commentaire, m.NOMMILIEUBIOLOGIQUE AS milieu, c.NOMCOMMUNE AS commune, c.CODEPOSTAL AS cp, p.NOMPAYS AS pays, g.COORDONNEEX AS gpsX, g.COORDONNEEY AS gpsY, g.COORDONNEEZ AS gpsZ, a.CODEETATPHENOLOGIQUE AS idEtat, a.CODETAXON AS idTaxon, e.NOMETATPHENOLOGIQUE AS etat, a.POPULATION AS population, fam.NOMFAMILLE AS famille, gen.NOMGENRE AS genre, esp.NOMESPECE AS espece, var.NOMVARIETE AS variete ";
	$query_recuperation_visite .= "FROM visite_dans_nature v ";
	$query_recuperation_visite .= "INNER JOIN milieu_biologique m ON m.CODEMILIEUBIOLOGIQUE = v.CODEMILIEUBIOLOGIQUE ";
	$query_recuperation_visite .= "LEFT JOIN commune c ON c.CODECOMMUNE = v.CODECOMMUNE ";
	$query_recuperation_visite .= "LEFT JOIN pays p ON p.CODEPAYS = c.CODEPAYS ";
	$query_recuperation_visite .= "LEFT JOIN coordonnees g ON g.CODECOORDONNEES = v.CODECOORDONNEES ";
	$query_recuperation_visite .= "LEFT JOIN a_ete_vu_taxon a INNER JOIN etat_phenologique e ON e.CODEETATPHENOLOGIQUE = a.CODEETATPHENOLOGIQUE ON a.CODEVISITEDANSNATURE = v.CODEVISITEDANSNATURE ";
	$query_recuperation_visite .= "LEFT JOIN taxon t ON t.CODETAXON = a.CODETAXON ";
	$query_recuperation_visite .= "LEFT JOIN est_de_famille l1 INNER JOIN famille fam ON fam.CODEFAMILLE = l1.CODEFAMILLE ON l1.CODETAXON = t.CODETAXON AND l1.CODECLASSIFICATION = t.CODECLASSIFICATION ";
	$query_recuperation_visite .= "LEFT JOIN appartient_a_genre l2 INNER JOIN genre gen ON gen.CODEGENRE = l2.CODEGENRE ON l2.CODETAXON = t.CODETAXON AND l2.CODECLASSIFICATION = t.CODECLASSIFICATION ";
	$query_recuperation_visite .= "LEFT JOIN est_de_espece l3 INNER JOIN espece esp ON esp.CODEESPECE = l3.CODEESPECE ON l3.CODETAXON = t.CODETAXON AND l3.CODECLASSIFICATION = t.CODECLASSIFICATION ";
	$query_recuperation_visite .= "LEFT JOIN a_comme_variete l4 INNER JOIN variete var ON var.CODEVARIETE = l4.CODEVARIETE ON l4.CODETAXON = t.CODETAXON AND l4.CODECLASSIFICATION = t.CODECLASSIFICATION ";
	$query_recuperation_visite .= "WHERE v.CODEVISITEDANSNATURE = ".$_GET['id']." ";
	
	$result_recuperation_visite = mysql_query($query_recuperation_visite, $connexion) or logError("RECUPERATION VISITE DANS NATURE-".$query_recuperation_visite."-".mysql_error());
	
	$visite = array(); // on créé un tableau contenant toutes les informations de la visite
	while($tab_recuperation_visite = mysql_fetch_assoc($result_recuperation_visite)){
	
		$visite['date'] = $tab_recuperation_visite['date'];
		$visite['rapport'] = $tab_recuperation_visite['rapport'];
		$visite['description'] = $tab_recuperation_visite['description'];
		$visite['commentaire'] = $tab_recuperation_visite['commentaire'];
		$visite['milieu'] = $tab_recuperation_visite['milieu'];
		$visite['commune'] = $tab_recuperation_visite['commune'];
		$visite['cp'] = $tab_recuperation_visite['cp'];
		$visite['pays'] = $tab_recuperation_visite['pays'];
		$visite['gpsX'] = $tab_recuperation_visite['gpsX'];
		$visite['gpsY'] = $tab_recuperation_visite['gpsY'];
		$visite['gpsZ'] = $tab_recuperation_visite['gpsZ'];
		if(!is_null($tab_recuperation_visite['idTaxon'])){
			$visite['taxons'][$tab_recuperation_visite['idTaxon']."-".$tab_recuperation_visite['idEtat']]['id'] = $tab_recuperation_visite['idTaxon'];
			$visite['taxons'][$tab_recuperation_visite['idTaxon']."-".$tab_recuperation_visite['idEtat']]['population'] = $tab_recuperation_visite['population'];
			$visite['taxons'][$tab_recuperation_visite['idTaxon']."-".$tab_recuperation_visite['idEtat']]['etat'] = $tab_recuperation_visite['etat'];
			$visite['taxons'][$tab_recuperation_visite['idTaxon']."-".$tab_recuperation_visite['idEtat']]['famille'] = $tab_recuperation_visite['famille'];
			$visite['taxons'][$tab_recuperation_visite['idTaxon']."-".$tab_recuperation_visite['idEtat']]['genre'] = $tab_recuperation_visite['genre'];
			$visite['taxons'][$tab_recuperation_visite['idTaxon']."-".$tab_recuperation_visite['idEtat']]['espece'] = $tab_recuperation_visite['espece'];
			$visite['taxons'][$tab_recuperation_visite['idTaxon']."-".$tab_recuperation_visite['idEtat']]['variete'] = $tab_recuperation_visite['variete'];
		}
	
	}
	
	mysql_free_result($result_recuperation_visite);
	
	// récupération des végétaux récoltés
	$query_recuperation_entrees_visite = "SELECT en.CODEENTREE AS idEntree, sa.CODESACHETGRAINE AS idSachet, sa.REFERENCESACHETGRAINES AS refSachet, se.CODESEMIS AS idSemis, se.REFERENCESEMIS AS refSemis, tou.CODETOUFFE AS idTouffe, tou.REFERENCETOUFFE AS refTouffe, fam.NOMFAMILLE AS famille, gen.NOMGENRE AS genre, esp.NOMESPECE AS espece, var.NOMVARIETE AS variete ";
	$query_recuperation_entrees_visite .= "FROM entree en ";
	$query_recuperation_entrees_visite .= "LEFT JOIN sachet_graines sa ON en.SAC_CODESACHETGRAINE = sa.CODESACHETGRAINE ";
	$query_recuperation_entrees_visite .= "LEFT JOIN semis se ON en.SEM_CODESEMIS = se.CODESEMIS ";
	$query_recuperation_entrees_visite .= "LEFT JOIN touffe tou ON en.TOU_CODETOUFFE = tou.CODETOUFFE ";
	$query_recuperation_entrees_visite .= "LEFT JOIN taxon t ON t.CODETAXON = sa.CODETAXON OR t.CODETAXON = se.CODETAXON OR t.CODETAXON = tou.CODETAXON ";
	$query_recuperation_entrees_visite .= "LEFT JOIN est_de_famille l1 INNER JOIN famille fam ON fam.CODEFAMILLE = l1.CODEFAMILLE ON l1.CODETAXON = t.CODETAXON AND l1.CODECLASSIFICATION = t.CODECLASSIFICATION ";
	$query_recuperation_entrees_visite .= "LEFT JOIN appartient_a_genre l2 INNER JOIN genre gen ON gen.CODEGENRE = l2.CODEGENRE ON l2.CODETAXON = t.CODETAXON AND l2.CODECLASSIFICATION = t.CODECLASSIFICATION ";
	$query_recuperation_entrees_visite .= "LEFT JOIN est_de_espece l3 INNER JOIN espece esp ON esp.CODEESPECE = l3.CODEESPECE ON l3.CODETAXON = t.CODETAXON AND l3.CODECLASSIFICATION = t.CODECLASSIFICATION ";
	$query_recuperation_entrees_visite .= "LEFT JOIN a_comme_variete l4 INNER JOIN variete var ON var.CODEVARIETE = l4.CODEVARIETE ON l4.CODETAXON = t.CODETAXON AND l4.CODECLASSIFICATION = t.CODECLASSIFICATION ";
	$query_recuperation_entrees_visite .= "WHERE en.CODEVISITEDANSNATURE = ".$_GET['id']." ";
	
	$result_recuperation_entrees_visite = mysql_query($query_recuperation_entrees_visite, $connexion) or logError("RECUPERATION ENTREES VISITE DANS NATURE-".$query_recuperation_entrees_visite."-".mysql_error());
	
	$visite['recolte'] = array(); // on créé un tableau contenant toutes les informations de la récolte de la visite
	while($tab_recuperation_entree_visite = mysql_fetch_assoc($result_recuperation_entrees_visite)){
	
		$visite['recolte'][$tab_recuperation_entree_visite['idEntree']]['taxon']['famille'] = $tab_recuperation_entree_visite['famille'];
		$visite['recolte'][$tab_recuperation_entree_visite['idEntree']]['taxon']['genre'] = $tab_recuperation_entree_visite['genre'];
		$visite['recolte'][$tab_recuperation_entree_visite['idEntree']]['taxon']['espece'] = $tab_recuperation_entree_visite['espece'];
		$visite['recolte'][$tab_recuperation_entree_visite['idEntree']]['taxon']['variete'] = $tab_recuperation_entree_visite['variete'];
		if(!is_null($tab_recuperation_entree_visite['idSachet'])){
			$visite['recolte'][$tab_recuperation_entree_visite['idEntree']]['sachet']['id'] = $tab_recuperation_entree_visite['idSachet'];
			$visite['recolte'][$tab_recuperation_entree_visite['idEntree']]['sachet']['reference'] = $tab_recuperation_entree_visite['refSachet'];
		}
		if(!is_null($tab_recuperation_entree_visite['idSemis'])){
			$visite['recolte'][$tab_recuperation_entree_visite['idEntree']]['semis']['id'] = $tab_recuperation_entree_visite['idSemis'];
			$visite['recolte'][$tab_recuperation_entree_visite['idEntree']]['semis']['reference'] = $tab_recuperation_entree_visite['refSemis'];
		}
		if(!is_null($tab_recuperation_entree_visite['idTouffe'])){
			$visite['recolte'][$tab_recuperation_entree_visite['idEntree']]['touffe']['id'] = $tab_recuperation_entree_visite['idTouffe'];
			$visite['recolte'][$tab_recuperation_entree_visite['idEntree']]['touffe']['reference'] = $tab_recuperation_entree_visite['refTouffe'];
		}
	
	}
	
	mysql_free_result($result_recuperation_entrees_visite);
	
?>
<h3>Détail d'une visite dans la nature</h3>
<table class="fiche" style="width: 100%;">
	<tr>
		<td colspan="2">
			<b>Visite du <?php echo dateFr($visite['date']); ?></b>
		</td>
	</tr>
	<tr>
		<th colspan="2">Informations</th>
	</tr>
	<tr>
		<td class="etiquette" style="width: 20%;">Lieu:</td>
		<td style="width: 80%;">
			<?php
				if(!is_null($visite['commune'])){
					echo htmlentities($visite['cp'], ENT_NOQUOTES, "UTF-8")." ".htmlentities(capitalise($visite['commune']), ENT_NOQUOTES, "UTF-8")." - ".htmlentities(mb_strtoupper($visite['pays']), ENT_NOQUOTES, "UTF-8");
				}else{
					echo "<i>Aucun lieu indiqué</i>";
				}
			?>
		</td>
	</tr>
	<tr>
		<td class="etiquette">Milieu:</td>
		<td>
			<?php echo htmlentities(mb_strtolower($visite['milieu']), ENT_NOQUOTES, "UTF-8"); ?>
		</td>
	</tr>
	<tr>
		<td class="etiquette">GPS:</td>
		<td>
			<?php
				if(!is_null($visite['gpsX'])){
					if(!is_null($visite['gpsZ'])){
						echo "(".$visite['gpsX']." ; ".$visite['gpsY']." ; ".$visite['gpsZ'].")";
					}else{
						echo "(".$visite['gpsX']." ; ".$visite['gpsY'].")";
					}
				}else{
					echo "<i>non-indiquées</i>";
				}
			?>
		</td>
	</tr>
	<tr>
		<th colspan="2">Description</th>
	</tr>
	<tr>
		<td colspan="2">
			<?php
				if(!is_null($visite['description'])){
					echo nl2br(htmlentities($visite['description'], ENT_NOQUOTES, "UTF-8"));
				}else{
					echo "<i>aucune description</i>";
				}
			?>
		</td>
	</tr>
	<tr>
		<th colspan="2">Rapport</th>
	</tr>
	<tr>
		<td class="etiquette">Rapport:</td>
		<td>
			<?php
				if(!is_null($visite['rapport'])){
					echo nl2br(htmlentities($visite['rapport'], ENT_NOQUOTES, "UTF-8"));
				}else{
					if(strtotime($visite['date']) < time()){
						echo "<i>aucun rapport sur la visite</i>";
					}else{
						echo "<i>la visite n'a pas encore eu lieu</i>";
					}
				}
			?>
		</td>
	</tr>
	<tr>
		<td class="etiquette">Taxons obs.:</td>
		<td>
			<?php
				if(isset($visite['taxons'])){
					echo "<ul>";
					foreach($visite['taxons'] as $taxon){
						echo "<li>".$taxon['population']." <a href=\"popup.php?p=gestion/taxon_affiche_popup&amp;id=".$taxon['id']."\" onclick=\"popup(this.href, 640, 480); return false;\"><b>".htmlentities(capitalise($taxon['famille']), ENT_NOQUOTES, "UTF-8")." ".htmlentities(capitalise($taxon['genre']), ENT_NOQUOTES, "UTF-8")." ".htmlentities(capitalise($taxon['espece']), ENT_NOQUOTES, "UTF-8")." ".htmlentities(capitalise($taxon['variete']), ENT_NOQUOTES, "UTF-8")."</b></a>, à l'état <b>".htmlentities(mb_strtolower($taxon['etat']), ENT_NOQUOTES, "UTF-8")."</b></li>";
					}
					unset($taxon);
					echo "</ul>";
				}else{
					if(strtotime($visite['date']) < time()){
						echo "<i>aucun taxon observé</i>";
					}
				}
			?>
		</td>
	</tr>
	<tr>
		<td class="etiquette">Récolte:</td>
		<td>
			<?php
				if(count($visite['recolte']) > 0){
					echo "<ul>";
					foreach($visite['recolte'] as $vegetal){
						echo "<li>";
						if(isset($vegetal['sachet'])){
							echo "<b><a href=\"popup.php?p=gestion/sachetgraines_affiche_popup&amp;id=".$vegetal['sachet']['id']."\" onclick=\"popup(this.href, 640, 480); return false;\">Sachet de graines";
							if(!is_null($vegetal['sachet']['reference'])){
								echo " (n° ent. ".htmlentities($vegetal['sachet']['reference'], ENT_NOQUOTES, "UTF-8").")";
							}
							echo "</a></b>";
						}else if(isset($vegetal['semis'])){
							echo "<b><a href=\"popup.php?p=gestion/semis_affiche_popup&amp;id=".$vegetal['semis']['id']."\" onclick=\"popup(this.href, 640, 480); return false;\">Semis";
							if(!is_null($vegetal['semis']['reference'])){
								echo " (n° ent. ".htmlentities($vegetal['semis']['reference'], ENT_NOQUOTES, "UTF-8").")";
							}
							echo "</a></b>";
						}else if(isset($vegetal['touffe'])){
							echo "<b><a href=\"popup.php?p=gestion/touffe_affiche_popup&amp;id=".$vegetal['touffe']['id']."\" onclick=\"popup(this.href, 640, 480); return false;\">Touffe";
							if(!is_null($vegetal['touffe']['reference'])){
								echo " (n° ent. ".htmlentities($vegetal['touffe']['reference'], ENT_NOQUOTES, "UTF-8").")";
							}
							echo "</a></b>";
						}
						
						echo " - ".htmlentities(capitalise($vegetal['taxon']['famille']), ENT_NOQUOTES, "UTF-8")." ".htmlentities(capitalise($vegetal['taxon']['genre']), ENT_NOQUOTES, "UTF-8")." ".htmlentities(capitalise($vegetal['taxon']['espece']), ENT_NOQUOTES, "UTF-8")." ".htmlentities(capitalise($vegetal['taxon']['variete']), ENT_NOQUOTES, "UTF-8");
						echo "</li>";
					}
					unset($vegetal);
					echo "</ul>";
				}else{
					if(strtotime($visite['date']) < time()){
						echo "<i>aucun végétal récolté</i>";
					}
				}
			?>
		</td>
	</tr>
	<tr>
		<th colspan="2">Commentaire</th>
	</tr>
	<tr>
		<td class="texte" colspan="2"><?php echo nl2br(htmlentities($visite['commentaire'], ENT_NOQUOTES, "UTF-8")); ?></td>
	</tr>
</table>
<?php
//------------------------------------------------------------ Accès refusé à la page

			}else{
				afficherStatut("La visite dans la nature n'existe pas", 3); // appel à la fonction afficherStatut() définie dans fonctions.php
			}

		}else{
			afficherStatut("La visite dans la nature n'est pas ou mal indiquée", 3); // appel à la fonction afficherStatut() définie dans fonctions.php
		}

	}else{
		if(isset($_SESSION)){
			include("pages/401.htm");
		}
	}

?>